package hk.ces.oxfam.patch.util;

import java.util.Calendar;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.RichTextString;

/**
 *
 * @author ReutyCheng
 */
public class POIHelper
{
  public static HSSFRow createRow(HSSFRow row, Object[] objects)
  {
    int column = 0;

    if (objects != null && objects.length > 0)
    {
      for (Object object : objects)
      {
        HSSFCell cell = row.createCell(column++);

        if (object instanceof String)
        {
          cell.setCellValue((String) object);
        }
        else if (object instanceof Double)
        {
          cell.setCellValue(((Double) object).doubleValue());
        }
        else if (object instanceof Date)
        {
          cell.setCellValue((Date) object);
        }
        else if (object instanceof Calendar)
        {
          cell.setCellValue((Calendar) object);
        }
        else if (object instanceof Boolean)
        {
          cell.setCellValue((Boolean) object);
        }
        else if (object instanceof RichTextString)
        {
          cell.setCellValue((RichTextString) object);
        }
        else
        {
          cell.setCellValue(object != null ? object.toString() : "");
        }
      }
    }

    return row;
  }
}
